#include "CmdTransfer.h"

CCmdTransfer::CCmdTransfer()
{
}

CCmdTransfer::~CCmdTransfer()
{

}

//函数名：Init
//函数功能:CCmdTransfer类初始化函数
//函数参数：无
//返回值:正确返回true，否则返回false
bool CCmdTransfer::Init()
{
    InfoContainer = CInfoContainer::GetInstance();
    if(!InfoContainer)
        return false;

    WeiQueue = CWeiQueue::GetInstance();
    if(!WeiQueue)
        return false;

    Queue = CQueue::GetInstance();
    if(!Queue)
        return false;

    WeiBo = CWeiBo::GetWeiboInstance();
    if(!WeiBo)
         return false;

    DevCreater = new CDevCreater();
    if(!DevCreater)
        return false;

    return true;
}

//函数名：Cleanup
//函数功能:反初始化函数
//函数参数：无
//返回值:无
void CCmdTransfer::Cleanup()
{
    InfoContainer->Cleanup();

    WeiQueue->Cleanup();

    Queue->Cleanup();

    WeiBo->Cleanup();

    if(DevCreater)
    {
        delete DevCreater;
        DevCreater = 0;
    }
}

//函数名：WeiCmdAnalyse
//函数功能:微博命令分析函数
//函数参数：无
//返回值:无
void CCmdTransfer::WeiCmdAnalyse()
{

    string weiboStatus = "把主人房间的空调关了吧。";

    //具体算法应该有改进余地
    if(weiboStatus.find("不在家") != weiboStatus.npos ||
            weiboStatus.find("看好家") != weiboStatus.npos)
    {
        //把所有房间设为自动
        cout << "自动" << endl;
        ;
    }
    else if(weiboStatus.find("回家") != weiboStatus.npos||
            weiboStatus.find("关闭智能模式") != weiboStatus.npos)
    {
        //把所有房间设为手动
        cout << "手动动" << endl;
        ;
    }
    else if(weiboStatus.find("灯")  != weiboStatus.npos )
    {  //关于灯的控制
        int startPos;
        int endPos = weiboStatus.find("的");
        string roomName;

        if( startPos = weiboStatus.find("关掉")){
            roomName = weiboStatus.substr(startPos + 2, endPos - 2 - startPos);
            //把房间的灯关掉
            cout << "关灯" << roomName << endl;
        }
        else if(startPos = weiboStatus.find("打开"))
        {
            roomName = weiboStatus.substr(startPos + 2, endPos - 2 - startPos);
            //把房间的灯打开
            cout << "开灯" << roomName << endl;
        }
    }
    else if(weiboStatus.find("空调")  != weiboStatus.npos)
    {
        int startPos;
        int endPos = weiboStatus.find("的空调");
        string roomName;

        if( (startPos = weiboStatus.find("关")) != weiboStatus.npos )
        {
            startPos = weiboStatus.find("把");
            roomName = weiboStatus.substr(startPos + 2, endPos - 2 - startPos);
            //把房间的空调关掉
            cout << "关空调" << roomName << endl;
        }
        else if( (startPos = weiboStatus.find("开")) != weiboStatus.npos )
        {
            startPos = weiboStatus.find("把");
            roomName = weiboStatus.substr(startPos + 2, endPos - 2 - startPos);
            //把房间的空调打开
        }
        else if(weiboStatus.find("速度") != weiboStatus.npos)
        {
            startPos = weiboStatus.find("把");
            roomName = weiboStatus.substr(startPos + 2, endPos - 2 - startPos);

            //找到速度
            startPos = weiboStatus.find("设为");
            string speed = weiboStatus.substr(startPos + 4, 1);

            //设定风速

        }
        else if(weiboStatus.find("温度") != weiboStatus.npos)
        {
            startPos = weiboStatus.find("把");
            roomName = weiboStatus.substr(startPos + 2, endPos - 2 - startPos);

            //找到温度
            startPos = weiboStatus.find("设为");
            string tem = weiboStatus.substr(startPos + 4, 2);

            //设定温度
        }
    }

}

//函数名：SerSmart
//函数功能:设置智能模式标志
//函数参数：(in)const int &RoomNum:要设置的房间号, (in)const int &CurFlag：当前需要设置的标记位, (in)int *RoomFlag：房间标志数据
//返回值:正确返回true，否则返回false
bool CCmdTransfer::SetSmart(const int &RoomNum, const int &CurFlag, int *RoomFlag)
{
    if(RoomNum <= InfoContainer->GetRoom() && RoomNum > 0)
        RoomFlag[RoomNum] = CurFlag;
    else
        return false;
    return true;
}

//函数名：SetHand
//函数功能:手动模式下，手动设置设备的控制命令
//函数参数：（in）const string & ZibeeID：设备的ZigbeeID， （in）const int Value：设备的设置参数
//返回值:无
void CCmdTransfer::SetHand(const string &ZigbeeID, const int & Value)
{
    int DevType;
    string Command;
    CTerminalDev * TerminalDev = NULL;
    InfoContainer->GetTerminalTypeInfo(ZigbeeID, DevType);
    TerminalDev = DevCreater->CreateTerminalDev(DevType);
    TerminalDev->Set(Value, Command);
    Queue->Push(Command);

    if(TerminalDev)
    {
        delete TerminalDev;
        TerminalDev = NULL;
    }
}

//函数名：GetSensorData
//函数功能:获取某个传感器设备的数据
//函数参数：（in）const string & ZigbeeID：传感器设备的ZigbeeID
//返回值:无
void CCmdTransfer::GetSensorData(const string &ZigbeeID)
{

}

//函数名：GetTerminalStatus
//函数功能:获取终端设备状态
//函数参数：(in)const int & RoomNum:获取状态的房间号
//返回值:无
void CCmdTransfer::GetTerminalStatus(const int &RoomNum)
{

}

//函数名:
//函数功能:
//函数参数：
//返回值:
bool CCmdTransfer::Run(int *RoomFlag)
{
    cout << "CmdTransfer Run..." << endl;
    SetHand("8000", 'H');
    return true;
}
